package com.copera.cn.kaka;

import org.json.JSONObject;

import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.HttpUtils;

public class ExchangeConfirmActivity extends BaseActivity{
	private String url ;
	private Button confirm,cancel;
	private RelativeLayout RelativeLayout01;
	private TextView pointCount,exchangeContent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_exchange_ex);
		init();
		initEvent();
		initValue();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(ExchangeConfirmActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	public void init(){
		url = getIntent().getStringExtra("url");
		confirm=(Button)findViewById(R.id.loginButton);
		RelativeLayout01 = (RelativeLayout) findViewById(R.id.RelativeLayout01);
		cancel = (Button)findViewById(R.id.loginButton1);
	}
	public void initEvent(){
		confirm.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				confirm();
			}
		});
		RelativeLayout01.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
	}
	//获取兑换规则
	private void initValue(){
		new AsyncTask<String, Integer, String[]>() {
  			@Override
  			protected  String[] doInBackground(String... params) {
  				try {
  					int num1 = url.indexOf("&&ruleId");
  					int num2 = url.indexOf("&&shopId");
  					String json = HttpUtils.get(ActionUrls.URL_EXCHANGERULE_FINDRULE+"?"+url.subSequence(num1+2, num2), getAppCustomer().getHttpClient());
					JSONObject jo = new JSONObject(json);
					String[] arr = new String[]{jo.getString("pointCount"),jo.getString("exchangeContent")};
					return arr;
  				} catch (Exception e) {
  					e.printStackTrace();
  					return null;
  				}
  			}
  			protected void onPostExecute(String[] arr) {
  				pointCount = (TextView)findViewById(R.id.textView1);
  				exchangeContent = (TextView)findViewById(R.id.textView4);
  				pointCount.setText(arr[0]);
  				exchangeContent.setText(arr[1]);
			}
  		}.execute("");
	}
	private void confirm() {

		new AsyncTask<String, Integer, JSONObject>() {

			@Override
			protected JSONObject doInBackground(String... params) {
				JSONObject jsonObj = null;
				try {
					String jsonStr = HttpUtils.get(params[0], getAppCustomer()
							.getHttpClient());

					if (jsonStr != null) {
						jsonObj = new JSONObject(jsonStr);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return jsonObj;
			}

			@Override
			protected void onPostExecute(JSONObject result) {

				if (result != null) {
					try {
						
						String message = result.getString("message");
						if(message.equals("success")){
							getAppCustomer().getShop().setExchangeToday(result.getString("exchangeToday"));
							Message msg = handler.obtainMessage(ActionUrls.FAILURE, "兑换成功");
							handler.sendMessage(msg);
							finish();
						}else{
							
							Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
							handler.sendMessage(msg);
						}
					} catch (Exception e) {
						e.printStackTrace();
						if (e.getMessage() != null) {

						}
					}
				}
			}

		}.execute(url);
	}
}
